<template>

  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">

        <!-- 顶部筛选表单和按钮 -->

        <el-form :inline="true" :model="TSForm" class="demo-form-inline" size="small">
          <el-row>
            <el-col :span="12"><div class="grid-content bg-purple">
                <el-form-item label="关键字">
                  <el-input v-model="TSForm.keyword" placeholder="根据文章标题搜索"></el-input>
                </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="TSForm.state" placeholder="请选择">
                  <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button size="small" @click="clearSubForm">清除</el-button>
                <el-button size="small" type="primary" @click="searchSub">搜索</el-button>
              </el-form-item>
            </div></el-col>
            <el-col :span="12"><div class="grid-content bg-purple-light" style="text-align: right;">
              <el-button class="el-button el-button--success el-button--small" @click="addInterArt">
                <i class="el-icon-edit"></i>
                新增技巧
            </el-button>
            </div></el-col>
          </el-row>
        </el-form>

        <!-- 表格总数据提示 -->
        <el-alert
          type="info"
          :closable="false"
          style="padding:8px 16px;margin-bottom:15px"
          show-icon>
          <template #default>
            数据一共{{total}}条
          </template>
        </el-alert>

        <!-- 表格 -->
        <el-table
          stripe
          border
          :data="interviewList"
          style="width: 100%">
          <el-table-column
            type="index"
            :index="indexMethod"
            label="序号"
            width="80">
          </el-table-column>
          <el-table-column
            prop="title"
            label="文章标题"
            width="400">
            <template #default={row}>
              {{row.title}}
              <el-link @click="clickVideo(row)" style="font-size:18px;color: #00f;" v-if="row.videoURL" :underline="false" type="primary">
                <i class="el-icon-film"></i>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column
            prop="visits"
            width="243"
            label="阅读数">
          </el-table-column>
          <el-table-column
            width="241"
            prop="username"
            label="录入人">
          </el-table-column>
          <el-table-column
            prop="createTime"
            width="241"
            label="录入时间">
            <template #default="{row}">
              {{row.createTime | parseTimeByString()}}
            </template>
          </el-table-column>
          <el-table-column
            prop="state"
            width="100"
            label="状态">
            <template #default={row}>
              已{{row.state | number2Text(status)}}
            </template>
          </el-table-column>
          <el-table-column
            width="180"
            label="操作">
              <template #default="{row}">
                <el-button type="text" @click="clickPreview(row)">预览</el-button>
                <el-button type="text" @click="clickType(row)">{{row.state === 0 ? '开启' : '禁用'}}</el-button>
                <el-button type="text" :disabled="row.state === 1 ? true : false " @click="modifySub(row)">修改</el-button>
                <el-button :disabled="row.state === 1 ? true : false " type="text" @click="delSub(row)">删除</el-button>
              </template>
          </el-table-column>
        </el-table>

        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          background
          :page-sizes="[1,5,10, 20, 50]"
          :page-size="size"
          layout="prev,pager, next, sizes, jumper "
          style="text-align:right"
          :total="total">
        </el-pagination>

      </el-card>
    </div>
    <!-- 添加和修改文章的dialog -->
    <interviewArtDialog ref="editDialog" :clickArtInfo="clickArtInfo" :showArtDialog.sync="showArtDialog"></interviewArtDialog>

    <!-- 预览文章的dialog -->
    <previewArtDialog :clickArtInfo="clickArtInfo" :showPreviewDialog.sync="showPreviewDialog"></previewArtDialog>

    <!-- 视频弹出层  -->
    <!-- :class="{active:showVideo}" -->
    <transition name="fade">
      <div class="video-preview" v-if="showVideo">
        <div class="delbutton" @click.stop="showVideo=false">
          <span style="font-size:20px;color:#fff">
            <i class="el-icon-close"></i>
          </span>
        </div>

        <div class="videoBox">
          <video style="width:100%;height:100%" autoplay="autoplay" controls="controls" :src="VideoURL"></video>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
// ajax
import { list, changeState, remove } from '@/api/hmmm/articles'
// 常量
import { status } from '@/api/hmmm/constants'
// 组件
import interviewArtDialog from '../components/interview-artDialog.vue'
import previewArtDialog from '../components/interview-previewDialog.vue'
export default {
  components: {
    interviewArtDialog,
    previewArtDialog
  },
  data () {
    return {
      // 顶部筛选双向绑定
      formInline: {
        keyword: '',
        state: null
      },
      TSForm: {
        keyword: '',
        state: null
      },
      // 表格双向绑定
      interviewList: [],
      page: 1,
      size: 10,
      total: 0, // 获取到总文章数
      status, // 文章状态常量
      showArtDialog: false, // 添加文章的常量
      clickArtInfo: {}, // 点击修改时对应文章的数据
      showPreviewDialog: false, // 预览dialog显示隐藏
      showVideo: false,
      VideoURL: ''
    }
  },
  created () {
    this.getInterviewList()
  },
  methods: {
    // 一加载得到列表数据
    async getInterviewList () {
      const { data: res } = await list({ ...this.formInline, page: this.page, pagesize: this.size })
      this.total = res.counts
      this.interviewList = res.items
    },
    // 当前页文章数量变化触发
    handleSizeChange (size) {
      this.size = size
      this.page = 1
      this.getInterviewList()
    },
    // 文章页码发生变化触发
    handleCurrentChange (page) {
      this.page = page
      this.getInterviewList()
    },
    // 点击弹出新增技巧弹窗
    addInterArt () {
      this.showArtDialog = true
    },
    // 点击修改
    modifySub (row) {
      this.showArtDialog = true
      this.clickArtInfo = row
      this.$nextTick(() => {
        this.$refs.editDialog.getArtInfoView()
      })
    },
    // 点击筛选文章
    searchSub () {
      this.formInline = {
        ...this.TSForm
      }
      this.page = 1
      this.getInterviewList()
    },
    // 清除文章
    clearSubForm () {
      this.TSForm = {
        keyword: '',
        state: null
      }
    },
    // 点击预览
    clickPreview (row) {
      this.showPreviewDialog = true
      this.clickArtInfo = row
    },
    // 改变文章禁用状态
    async clickType (row) {
      row.state = row.state === 1 ? 0 : 1
      if (this.interviewList.length === 1 && this.page > 1) {
        this.page--
      }
      await changeState(row)
      this.getInterviewList()
      this.$message.success('操作成功！')
    },
    // 点击删除对应的文章
    delSub (row) {
      this.$confirm('是否永久的删除该文章?', '提示', { type: 'warning' })
        .then(async () => {
          await remove(row)
          if (this.interviewList.length === 1 || this.page > 1) {
            this.page--
          }
          this.getInterviewList()
          this.$message.success('删除文章成功！')
        })
        .catch(e => e)
    },
    // 点击视频按钮
    clickVideo (row) {
      this.VideoURL = row.videoURL
      this.showVideo = true
    },
    indexMethod (index) {
      return (this.page - 1) * this.size + index + 1
    }
  }
}
</script>

<style scoped lang='scss'>

.app-container {
  padding: 0 10px;
  margin: 10px 0;
}
::v-deep .el-form-item__label{
  width: 80px;
}
::v-deep .el-alert__icon.is-big{
    font-size: 16px;
}
.video-preview{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,.3);
    overflow: hidden;
    z-index: 9999;
}
.active{
  display: none;
}
.delbutton{
  position: absolute;
  top: 30px;
  left: 50%;
  width: 50px;
  height: 50px;
  background: rgba(0,0,0,.4);
  transform: translate(-50%);
  border-radius: 50%;
  text-align: center;
  line-height: 50px;
  box-shadow: 0 0 5px rgb(0 0 0 / 40%);
}
.videoBox{
    width: 695px;
    height: 400px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}
.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to{
  opacity: 0;
}
</style>
